<template>
  <div class="annex-main-box">
    <el-drawer
      :visible.sync="isDrawer"
      :direction="direction"
      :size="this.$store.state.user.userIsPhone ? '90%' : '400px'"
      :with-header="false"
      @close="defaultData"
    >
      <div class="drawer-title">
        <div class="drawer-title">修改附件信息</div>
        <div class="iconfont icon-guanbi" @click="isDrawer = false"></div>
      </div>
      <!-- <div class="head-box">
        <div class="identifier-box"></div>
        <div>附件信息</div>
      </div>
      <div class="info-box">
        <div class="left-box">
          <div>
            附件ID：
          </div>
          <div>
            附件名称：
          </div>
          <div>
            创建时间：
          </div>
          <div>
            创建人：
          </div>
          <div>
            附件：
          </div>
        </div>
        <div class="right-box">
          <div>
              附件ID
          </div>
          <div>
              <input type="text" v-model="fileName" />
          </div>
          <div>
              <input type="text" v-model="createTime" />
          </div>
          <div>
              <input type="text" v-model="createName" />
          </div>
          <div>
              附件
          </div>
        </div>
      </div> -->
      <div class="anne-box">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-success="uploadSuccess"
          :multiple="false"
          :file-list="fileList"
        >
          <el-button size="small" type="success">附件上传</el-button>
        </el-upload>
      </div>
      <div class="head-box" style="height: 0"></div>
      <div class="btn-box">
        <!-- <div class="download-btn">下载附件</div> -->
        <!-- <div class="sub-btn">提交</div>
        <div class="remove-btn">取消</div> -->
      </div>
    </el-drawer>
    <DialogPreview ref="dialogPreviewRef"/>
  </div>
</template>

<script>
import { getFilesListApi, delFilesApi } from '@/api/index'
import DialogPreview from './DialogPreview.vue'

export default {
  props: {
    drawer: {
      type: Boolean,
      default: false
    },
    projectRow: {
      type: Object,
      default: undefined
    }
  },
  data () {
    return {
      // 项目编号
      proCode: '',
      // 是否弹出抽屉
      isDrawer: false,
      // 抽屉弹出方向
      direction: 'rtl',
      uploadUrl: 'apis/api/FilesUpload',
      // 文件列表
      fileList: [],
      // 附件id
      fileCode: '',
      // 附件名称
      fileName: '',
      // 创建时间
      createTime: '',
      // 创建人
      createName: ''
    }
  },
  components: { DialogPreview },

  mounted () {},

  methods: {
    openDrawer (row) {
      this.proName = row.proshortname
      this.proCode = row.procode
      this.isDrawer = true
      this.getProFile(row.guid)
      this.uploadUrl = `apis/api/FilesUpload?detailguid=${row.guid}`
    },

    // 获取当前项目的文件列表
    async getProFile (code) {
      let data = {
        detailguid: code
      }
      let res = await getFilesListApi(data)
      this.fileList = []
      res.forEach(item => {
        this.fileList.push({
          name: item.name,
          url: item.guid,
          guid: item.guid
        })
      })
    },
    // 文件列表移除文件时的钩子
    handleRemove (file, fileList) {
      this.delProFile(file.guid)
    },
    // 点击文件列表中已上传的文件时的钩子
    async handlePreview (file) {
      this.$refs.dialogPreviewRef.showDialog(file)
      // let data = { guid: file.guid }
      // let res = await downloadFileApi(data)
      
      // console.log('点击的文件--->', file, res)
    },
    // 限制文件大小
    handleExceed (files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      )
    },
    // 文件上传成功
    uploadSuccess (response, file, fileList) {},
    // 文件删除之前
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    // 删除文件
    async delProFile (id) {
      let data = {
        guid: id
      }
      let res = await delFilesApi(data)
    },

    defaultData () {
      this.fileList = []
    }
  }
}
</script>

<style lang="scss" scoped>
.annex-main-box {
  .drawer-title {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    margin-left: 5px;
    padding: 5px;
    padding-right: 15px;
    font-size: 16px;
    > div {
      font-weight: bold;
    }
    .icon-guanbi:hover {
      cursor: pointer;
    }
  }
  .head-box {
    display: flex;
    align-items: center;
    height: 30px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding-left: 20px;
    font-size: 13px;
    line-height: 30px;
    text-align: start;
    border-bottom: 1px solid #dddcdc;
    .identifier-box {
      width: 5px;
      height: 10px;
      margin-right: 6px;
      background: #11a70c;
    }
  }
  .info-box {
    display: flex;
    padding-left: 8%;

    /* border: 1px solid red; */
    >div {
      height: 100%;

      /* border: 1px solid green; */
      >div {
        display: flex;
        align-items: center;
        justify-content: space-around;
        height: 30px;
      }
    }
    .left-box {
      width: 25%;
      >div {
        justify-content: flex-end;
      }
    }
    .right-box {
      width: 60%;
    }
  }
  .anne-box {
    display: flex;

    /* border: 1px solid red; */
    width: 100%;
    margin-top: 10px;
    padding-left: 8%;
    .upload-demo {
      /* border: 1px solid red; */
      width: 100%;
      text-align: start;
      .upload-btn {
        color: #000;
        background: rgba($color: #000, $alpha: 0%);
        border: none;
      }
      > ul::-webkit-scrollbar {
        width: 4px;
        height: 8px;
      }
      > ul::-webkit-scrollbar-thumb {
        background: fade(rgb(78 182 214), 60%);
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
        opacity: 0.2;
      }
      > ul::-webkit-scrollbar-track {
        background: fade(rgb(78 182 214), 30%);
        border-radius: 0;
        box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
      }
    }
  }
  .btn-box {
    display: flex;
    justify-content: end;
    width: 100%;
    margin: 10px 0;
    padding-right: 10px;

    /* border: 1px solid #c40f0f; */
    > div {
      width: 60px;
      height: 32px;
      margin-left: 10px;
      color: #fff;
      font-size: 12px;
      line-height: 32px;
      text-align: center;
      background: #85ce61;
      border-radius: 5px;
    }
    > div:hover {
      background: rgba($color: #85ce61, $alpha: 80%);
      cursor: pointer;
    }
    .download-btn {
      width: 80px;
      background: #409Efe;
      &:hover {
        background: rgba($color: #409Efe, $alpha: 80%);
      }
    }
    .sub-btn {
      color: #fff;
      background: #67c23a;
    }
    .sub-btn:hover {
      color: #fff;
      border-color: #85ce61;
      cursor: pointer;
    }
    .remove-btn {
      color: #000;
      background: rgba($color: #000, $alpha: 0%);
      border: 1px solid #dcdfe6;
    }
    .remove-btn:hover {
      color: #409eff;
      background-color: #ecf5ff;
      border-color: #c6e2ff;
      cursor: pointer;
    }
  }
}

@media screen and (width <= 768px) {
  .main-box {
    background: #ee0909;
  }
}
</style>